//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.restaurant.application.indexes.accumulatelist;

import com.google.common.collect.Lists;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.DateTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.TableShell;
import com.nebutown.commonunits.dataface.definitions.FieldValue;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.dataface.definitions.PageInfo;
import com.nebutown.commonunits.dataface.definitions.TimeInterval;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.restaurant.application.indexes.accumulatelist.mysql.AccumulateListTableBean;
import com.restaurant.application.indexes.accumulatelist.mysql.AccumulateListTableCriteria;
import com.restaurant.application.indexes.accumulatelist.mysql.AccumulateListTableImpl;
import com.restaurant.application.indexes.accumulatelist.support.AbstractAccumulateList;
import com.restaurant.application.indexes.accumulatelist.support.AccumulateSearch;
import com.restaurant.application.util.CommonUtils;
import java.util.List;
import java.util.Objects;

public class AccumulateListImpl extends AbstractAccumulateList {
    public AccumulateListImpl() {
    }

    public View getFullView() {
        return this.newCommonTable(new AccumulateSearch());
    }

    public View getViewLead(ViewSpec spec) {
        return null;
    }

    public ViewUpdate doSearch(AccumulateSearch doSearch, int[] seq) {
        return this.returnView(new View[]{this.newCommonTable(doSearch)});
    }

    private View newCommonTable(AccumulateSearch criteria) {
        int pageNum = Objects.nonNull(criteria.getPage()) ? criteria.getPage().intValue() : 1;
        int pageSize = 10;
        int start = (pageNum - 1) * pageSize;
        int stop = pageNum * pageSize;
        int totalRecord = ((AccumulateListTableImpl)this.getIndex(AccumulateListTableImpl.class)).count(this.getSearchBean(criteria));
        int totalPage = (totalRecord + pageSize - 1) / pageSize;
        List<AccumulateListTableBean> goodsList = ((AccumulateListTableImpl)this.getIndex(AccumulateListTableImpl.class)).search(this.getSearchBean(criteria), CommonUtils.getSort("CREATETIME"), start, stop);
        PageInfo pageInfo = new PageInfo(Integer.valueOf(pageSize), pageNum, totalPage, totalRecord, this.getPath(CMD_DOSEARCH, new int[0]), "page");
        PageForm pageForm = this.newFormShell(criteria, goodsList, pageInfo).getForm();
        return ((UIDataFace)this.getSingleton(UIDataFace.class)).newUniForm(this.unitPosition(), pageForm);
    }

    private FormShell newFormShell(AccumulateSearch criteria, List<AccumulateListTableBean> goodsList, PageInfo pageInfo) {
        FormShell formShell = new FormShell("积分消费记录/积分变动查询", 3, 5);
        formShell.source(criteria, DataModes.EDIT);
        formShell.at(0, 0, "手机号").stringField("phone", criteria.getPhone()).placeHolder("手机号").setEditable(true);
        formShell.at(0, 1, "会员名字").stringField("nickName", criteria.getNickName()).placeHolder("会员名字").setEditable(true);
        formShell.at(1, 0, "会员编号").stringField("customerId", criteria.getCustomerId()).placeHolder("会员编号").setEditable(true);
        List<String> list = Lists.newArrayList(new String[]{"兑换减少", "消费增加", "签到增加", "减少积分-人工", "增加积分-人工"});
        formShell.at(1, 1, "类型").selectEntry("type", criteria.getType(), list).placeHolder("类型").setEditable(true);
        formShell.at(2, 0, "时间").intervalField("time", criteria.getTime(), DateTypes.DATETIME).placeHolder("时间").setEditable(true);
        formShell.at(2, 1, "").buttonField(this.getPath(CMD_DOSEARCH, new int[0]), "搜索", "", true);
        TableShell tableShell = (new TableShell(AccumulateListTableBean.class, goodsList)).withPageInfo(pageInfo);
        tableShell.createColumn("createTime").withName("时间").cellCalculator((b) -> {
            AccumulateListTableBean bean = (AccumulateListTableBean)b;
            return (new FieldValue()).withValue(DateTimeUtils.toTime(bean.getCreateTime()));
        });
        tableShell.createColumn("type").withName("类型").cellCalculator((b) -> {
            AccumulateListTableBean bean = (AccumulateListTableBean)b;
            return (new FieldValue()).withValue(bean.getType());
        });
        tableShell.createColumn("points").withName("积分").cellCalculator((b) -> {
            AccumulateListTableBean bean = (AccumulateListTableBean)b;
            return bean.getType().contains("增加") ? (new FieldValue()).withValue("+" + bean.getPoints().toString()) : (new FieldValue()).withValue("-" + bean.getPoints().toString());
        });
        tableShell.createColumn("currentPoints").withName("当前积分").cellCalculator((b) -> {
            AccumulateListTableBean bean = (AccumulateListTableBean)b;
            return (new FieldValue()).withValue(bean.getCurrentPoint().toString());
        });
        tableShell.createColumn("customerId").withName("会员编号").cellCalculator((b) -> {
            AccumulateListTableBean bean = (AccumulateListTableBean)b;
            return (new FieldValue()).withValue(bean.getCustomerId());
        });
        tableShell.createColumn("customerName").withName("会员名字").cellCalculator((b) -> {
            AccumulateListTableBean bean = (AccumulateListTableBean)b;
            return (new FieldValue()).withValue(bean.getCustomerName());
        });
        tableShell.createColumn("phone").withName("手机号").cellCalculator((b) -> {
            AccumulateListTableBean bean = (AccumulateListTableBean)b;
            return (new FieldValue()).withValue(bean.getPhone());
        });
        formShell.at(4, 0, "积分明细表").large(3, 1).tableField(tableShell);
        return formShell;
    }

    private AccumulateListTableCriteria getSearchBean(AccumulateSearch search) {
        if (Objects.isNull(search.getTime())) {
            search.withTime(new TimeInterval());
        }

        return (new AccumulateListTableCriteria()).withPhoneLike(CommonUtils.getNullStr(search.getPhone())).withCustomerNameLike(CommonUtils.getNullStr(search.getNickName())).withCustomerIdLike(CommonUtils.getNullStr(search.getCustomerId())).withCreateTimeLower(search.getTime().getFrom()).withCreateTimeUpper(search.getTime().getTo()).withTypeLike(CommonUtils.getNullStr(search.getType()));
    }
}
